package com.util;

import lombok.Data;


/**
 * Filename:    CodeMsg.java
 * Description:
 * Copyright:   Copyright (c) 2022-2025 All Rights Reserved.
 * Company:     chinasofti.com Inc.
 * @author:     mazheng
 * @version:    1.0
 * Create at:   2022-08-19
 *
 * Modification History:
 * Date         Author      Version     Description       tag
 * ------------------------------------------------------------------
 * 2022-08-19      jiangqi      1.0         1.0 Version
 * 2022-08-19      mazheng      1.0         1.0 Version      rev
 *
 */
@Data
public class Result<T> {
    private int code;//返回状态码
    private String msg;//返回的message
    private T data;//返回的数据

    private Result(T data) {
        this.code = 200;
        this.msg = "success";
        this.data = data;
    }

    private Result(      int code, String data) {
        this.code = code;
        this.msg = data;

    }

    private Result(CodeMsg msg) {
        if (msg == null) {
            return;
        }
        this.code = msg.getCode();
        this.msg = msg.getMsg();
    }

    /**
     * 失败的时候调用
     */
    public static <T> Result<T> error(CodeMsg msg) {
        return new Result<T>(msg);
    }

    /**
     * 成功的时候调用
     */
    public static <T> Result<T> success(T data) {
        return new Result<T>(data);
    }

    public static Result error(String defaultMessage) {
        return new Result(402, defaultMessage);
    }
}